import {
  wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();

const app = getApp()
Page({

  data: {
    imgUrl: app.globalData.imgUrl,
    infoList: [],
    storeItem: {},
    expireDate: '',
    expireTime: '',
    startDate:'',
    startTime:'',
    infoindex: ''
  },


  onLoad: function(options) {
    this.uploadImgs = this.selectComponent("#uploadImgs");
    console.log(options, 'options')

    if (options.id) {
      this.setData({
        id: options.id
      })
      this.getDetail()
    }


  },

  onShow: function() {
    this.getInfoType();
    this.getSotList()   
  },

  onUnload: function() {
    wx.removeStorageSync('newsItem')
  },

  onHide: function() {
    wx.removeStorageSync('newsItem')
  },

  //选择商品图片
  chooseGoodsImg: function(e) {
    this.setData({
      imgs: this.uploadImgs.data.imgs
    })
  },

  deleteGoodsImg: function(e) {
    this.setData({
      imgs: this.uploadImgs.data.imgs
    })
  },

  deloriginGoodsImg: function() {
    this.setData({
      originImgs: this.uploadImgs.data.originImgs
    })
  },


  //获取类型
  getInfoType: function() {
    var that = this;
    //请求
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/protected/comm/beanCrud/cms.InfoTypeVO/find',
      method: 'POST',
      data: {
        pageSize: 50
      }
    }, (res) => {
      console.log(res)
      var list = res.list
      if (that.data.cententDetail && that.data.cententDetail.id) {
        let infoindex = ''
        list.map((item, index) => {
          if (item.id == that.data.cententDetail.typeId) {
            infoindex = index;
          }
        })
        that.setData({
          infoindex
        })
      }
 
      this.setData({
        infoList: list
      })
    })
  },


  //选择类型
  infoPickerChange: function(e) {
    var infoindex = e.detail.value;
    var infoList = this.data.infoList
    var typeId = '';

    infoList.map((item, index) => {
      if (index == infoindex) {
        typeId = item.id
      }
    })
    console.log(infoindex, 'infoindex')
    this.setData({
      infoindex,
      typeId
    })
  },
 

  handleStore: function() {
    wx.navigateTo({
      url: '/pages/publish/template/store',
    })
  },

  //当编辑器内容发生改变获取编辑器内容
  onbindblur(e) {
    console.log(e, '编辑器')
    this.setData({
      editorContent: e.detail.html
    })
  },

  onCheckedChange(event) {
    this.setData({
      checked: event.detail
    });
  },


  //过期时间
  bindDateChange: function(e) {
    this.setData({
      expireDate: e.detail.value
    })
  },

  bindTimeChange: function(e) {
    this.setData({
      expireTime: e.detail.value
    })
  },

  // 开始时间
  bindStartDateChange: function (e) {
    this.setData({
      startDate: e.detail.value
    })
  },

  bindstartTimeChange: function (e) {
    this.setData({
      startTime: e.detail.value
    })
  },



  formSubmit: function(e) {
    var formdata = e.detail.value;
    var type=e.detail.target.dataset.type
    formdata.top = formdata.top ? 1 : 0
    formdata.typeId = this.data.typeId
    formdata.stoName = this.data.storeItem.stoName
    formdata.stoId = this.data.storeItem.id

    if (this.data.expireDate && this.data.expireTime){
      formdata.expireDate = this.data.expireDate + ' ' + this.data.expireTime + ':00'
    }

    if (this.data.startDate && this.data.startTime) {
       formdata.startDate = this.data.startDate + ' ' + this.data.startTime + ':00'
    }

    formdata.content = this.data.editorContent

    var imgArr = this.uploadImgs.data.datalist
    if (imgArr && imgArr.length > 0) {
      formdata.imgPath = imgArr[0].imgPath
      formdata.imgName = imgArr[0].imgName
    }

    if (!formdata.typeId) {
      wx.showToast({
        icon: 'none',
        title: '请选择广告类型',
      })
      return
    }

    if (!formdata.stoName) {
      wx.showToast({
        icon: 'none',
        title: '请选择店铺',
      })
      return
    }

    if (!formdata.title) {
      wx.showToast({
        icon: 'none',
        title: '标题不能为空',
      })
      return
    }

    if (formdata.expireDate && formdata.startDate){
      if (formdata.expireDate <= formdata.startDate) {
        wx.showToast({
          icon: 'none',
          title: '开始时间不能大于或等于过期时间',
        })
        return
      }
    }
    

    if (!formdata.imgPath) {
      wx.showToast({
        icon: 'none',
        title: '图片不能为空',
      })
      return
    }

    if (!formdata.content) {
      wx.showToast({
        icon: 'none',
        title: '内容详情不能为空',
      })
      return
    }  

    if (type =='view'){
      wx.setStorageSync('cmsformdata', formdata)
      wx.navigateTo({
        url: "/pages/activity/detail?type=view",
      })
    }else{
      this.formRequest(formdata)
    }

    
  },

  




  //请求
  formRequest: function(formdata) {
    var url = '/sys/protected/info/create';
    var title = "发布成功"

    //修改
    if (this.data.cententDetail && this.data.cententDetail.id) {
      url = "/sys/protected/info/update";
      formdata.id = this.data.cententDetail.id;
      title = "修改成功"
    }

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: formdata
    }, (res) => {
      console.log(res)
      wx.showToast({
        title,
      })
      setTimeout(() => {
        wx.redirectTo({
          url: '/pages/publish/cmsinfo/list',
        })
      }, 1500)
    })
  },

  //获取详情
  getDetail: function() {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/sys/info/getById?id=' + this.data.id,
      method: 'post'
    }, (res) => {
      console.log(res, '资讯详情')

      if (res) {
        this.setData({
          cententDetail: res,
          typeId: res.typeId,
          editorContent: res.content,
          startDate: res.startDate ? res.startDate.substring(0, 10) : '',
          startTime: res.startDate ? res.startDate.substring(11, 16) : '',

          expireDate: res.expireDate ? res.expireDate.substring(0, 10) : '',
          expireTime: res.expireDate ? res.expireDate.substring(11, 16) : '',

          storeItem: {
            id: res.stoId,
            stoName: res.stoName || '店铺名称'
          }
        })
        if (res.imgPath) {
          var imgArray = [{
            imgPath: res.imgPath,
            imgName: res.imgName
          }]

          this.uploadImgs.setData({
            datalist: imgArray,
            originImgs: imgArray
          })
        }

      }

    })
  },


  // 获取列表
  getSotList: function() {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + "/org/protected/bizOrg/findStoreList",
      method: "post",
      data: {
        keyword: that.data.keywords,
        status: 1,
      }
    }, function(res) {
      console.log(res,'getSotList')
      var list = res.list;
      if (list.length == 1) {
        that.setData({
          storeItem: {
            id: list[0].id,
            stoName: list[0].stoName
          }
        })
        
      }
    })
  },



})